<?php
namespace App\Libraries;

class Captcha
{

    private $code;
    private $charset = 'ABCDeEFGHIJKLM7NOPQRSTUV5WXYZabcd7efghijk8mnopqr4stuvwx6yz234567890'; //设置随机生成因子
    private $width;
    private $height;
    private $img;
    private $font;
    private $fontsize = 20;
    private $fontcolor;
    private $randstring = ['*', '@', '$', '%', '&', '!'];

    public function __construct(...$params)
    {
        $this->font = WEBPATH . 'assets/mokui/font/captcha.ttf';
    }

    // todo
    public function create($width = 120, $height = 35)
    {
        $this->_code();
        $this->width = $width ?? 120;
        $this->height = $height ?? 35;
        $this->_bg();
        $this->_line();
        $this->_font();
        $this->_show();
        return $this->code;
    }

    //生成随机验证码
    private function _code()
    {
        $code = '';
        $charset_len = strlen($this->charset) - 1;
        for ($i = 0; $i < 4; $i++) {
            $code .= $this->charset[rand(1, $charset_len)];
        }
        $this->code = strtolower($code);
    }

    //生成背景
    private function _bg()
    {
        $this->img = imagecreatetruecolor($this->width, $this->height);
        $color = imagecolorallocate($this->img, 255, 255, 255);
        imagecolortransparent($this->img, $color);
        imagefill($this->img, 0, 0, $color);
    }

    //生成文字
    private function _font()
    {
        $_x = $this->width / 4;
        for ($i = 0; $i < 4; $i++) {
            $this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 180), mt_rand(0, 180), mt_rand(0, 180));
            imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
        }
    }

    //生成干扰线条
    private function _line()
    {
        for ($i = 0; $i < 5; $i++) {
            $color = imagecolorallocate($this->img, mt_rand(0, 180), mt_rand(0, 180), mt_rand(0, 180));
            imageline(
                $this->img,
                mt_rand(0, $this->width),
                mt_rand(0, $this->height),
                mt_rand(0, $this->width),
                mt_rand(0, $this->height),
                $color
            );
        }
        for ($i = 0; $i < 20; $i++) {
            $color = imagecolorallocate($this->img, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255));
            imagestring(
                $this->img,
                mt_rand(1, 5),
                mt_rand(0, $this->width),
                mt_rand(0, $this->height),
                $this->randstring[rand(0, 5)],
                $color
            );
        }
    }

    //显示
    private function _show()
    {
        ob_clean();
        header('Content-type:image/png');
        imagepng($this->img);
        imagedestroy($this->img);
    }
}
